home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / shutdown / RCS / shutdown.c,v < prev    next >
Text File  |  1990-07-10  |  11KB  |  558 lines

  1. head     1.11;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    jhh:1.11; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.11
  10. date     90.01.24.07.46.05;  author douglis;  state Exp;
  11. branches ;
  12. next     1.10;
  13.  
  14. 1.10
  15. date     89.11.28.15.50.43;  author douglis;  state Exp;
  16. branches ;
  17. next     1.9;
  18.  
  19. 1.9
  20. date     89.06.19.14.33.33;  author jhh;  state Exp;
  21. branches ;
  22. next     1.8;
  23.  
  24. 1.8
  25. date     89.05.21.16.49.06;  author jhh;  state Exp;
  26. branches ;
  27. next     1.7;
  28.  
  29. 1.7
  30. date     88.09.26.11.26.43;  author nelson;  state Exp;
  31. branches ;
  32. next     1.6;
  33.  
  34. 1.6
  35. date     88.08.16.11.27.44;  author nelson;  state Exp;
  36. branches ;
  37. next     1.5;
  38.  
  39. 1.5
  40. date     88.08.16.11.03.56;  author nelson;  state Exp;
  41. branches ;
  42. next     1.4;
  43.  
  44. 1.4
  45. date     87.10.21.12.28.19;  author nelson;  state Exp;
  46. branches ;
  47. next     1.3;
  48.  
  49. 1.3
  50. date     87.09.15.19.55.40;  author nelson;  state Exp;
  51. branches ;
  52. next     1.2;
  53.  
  54. 1.2
  55. date     87.04.26.21.46.25;  author douglis;  state Exp;
  56. branches ;
  57. next     1.1;
  58.  
  59. 1.1
  60. date     87.02.18.14.03.40;  author douglis;  state Exp;
  61. branches ;
  62. next     ;
  63.  
  64.  
  65. desc
  66. @Program to shutdown and possibly reboot the system.
  67. @
  68.  
  69.  
  70. 1.11
  71. log
  72. @do local wall msg and wait a bit, by default.
  73. @
  74. text
  75. @/* 
  76.  * shutdown.c --
  77.  *
  78.  *    Program to shutdown the operating system.
  79.  *
  80.  * Copyright (C) 1988 Regents of the University of California
  81.  * All rights reserved.
  82.  */
  83.  
  84. #ifndef lint
  85. static char rcsid[] = "$Header: /sprite/src/cmds/shutdown/RCS/shutdown.c,v 1.10 89/11/28 15:50:43 douglis Exp Locker: douglis $ SP RITE (Berkeley)";
  86. #endif not lint
  87.  
  88. #include "sys.h"
  89. #include "option.h"
  90. #include <stdio.h>
  91. #include <sys/file.h>
  92. #include <errno.h>
  93.  
  94. #define FASTBOOT "/local/fastboot"
  95.  
  96. /*
  97.  * Options.
  98.  */
  99. static int    halt = 0;        /* Non-zero means halt. */
  100. static int    dontSyncDisks = 0;    /* Non-zero means don't sync the disks
  101.                      * when shutting down the system. */
  102. static int    reboot = 0;        /* Non-zero means reboot. */
  103. static int    debug = 0;        /* Non-zero means enter the debugger. */
  104. static int    fastBoot = 0;        /* Non-zero means don't check the disks
  105.                      * on reboot. */
  106. static int    quickBoot = 0;        /* Whether not to do wall. */
  107. static int    sleepTime = 30;        /* Number of seconds to sleep after
  108.                      * wall. */
  109. static int    singleUser = 0;        /* Non-zero means reboot single user. */
  110. static int    client    = 0;        /* Non-zero means reboot fileserver
  111.                      * without using /boot on local disk. */
  112. static int    rootcmds = 0;        /* Non-zero means run rootcmds. */
  113. static int    debugShutdown = 0;    /* Non-zero means don't really
  114.                      * shut down. */
  115. /*
  116.  * String to use when rebooting the system.
  117.  */
  118. char        nullString[] = "";
  119. char        *rebootString = nullString;
  120. char        buffer[100];
  121.  
  122. /*
  123.  * Flags to command-line options:
  124.  */
  125. Option optionArray[] = {
  126.     {OPT_TRUE, "h", (Address) &halt, "Halt (This is the default)"},
  127.     {OPT_TRUE, "r", (Address) &reboot, "Reboot"},
  128.     {OPT_STRING, "R", (Address) &rebootString, "String to pass to boot prom (implies -r)"},
  129.     {OPT_TRUE, "d", (Address) &debug, "Enter the debugger"},
  130.     {OPT_TRUE, "f", (Address) &fastBoot,
  131.          "Don't check disk consistency upon reboot (reboot if no other options.)"},
  132.     {OPT_TRUE, "w", (Address) &dontSyncDisks, "Dont write back the cache (Default is to write it back)"},
  133.     {OPT_TRUE, "s", (Address) &singleUser, "Reboot single user mode"},
  134.     {OPT_TRUE, "c", (Address) &client, 
  135.     "Reboot fileserver as a client (don't use /boot on local disk)"},
  136.     {OPT_TRUE, "x", (Address) &rootcmds, "Run rootcmds before diskcmds"},
  137.     {OPT_TRUE, "q", (Address) &quickBoot, "Don't do a wall, and wait, before rebooting"},
  138.     {OPT_INT, "S", (Address) &sleepTime, "Number of seconds to wait after wall"},
  139.     {OPT_TRUE, "D", (Address) &debugShutdown, "Don't actually shut down"},
  140. };
  141. int numOptions = sizeof(optionArray) / sizeof(Option);
  142.  
  143.  
  144. /*
  145.  *----------------------------------------------------------------------
  146.  *
  147.  * main --
  148.  *
  149.  *    The main program for shutdown.
  150.  *
  151.  * Results:
  152.  *    None.
  153.  *
  154.  * Side effects:
  155.  *    Prints information on standard output.
  156.  *
  157.  *----------------------------------------------------------------------
  158.  */
  159. main(argc, argv)
  160.     int argc;
  161.     char *argv[];
  162. {
  163.     int    flags;
  164.  
  165.     (void) Opt_Parse(argc, argv, optionArray, numOptions, OPT_ALLOW_CLUSTERING);
  166.     if (!quickBoot) {
  167.     char *args[3];
  168.     int fd[2];
  169.     int pid;
  170.     
  171.     sprintf(buffer, "system shutting down in %d seconds\n", sleepTime);
  172.     if (pipe(fd) < 0) {
  173.         perror("pipe");
  174.         exit(1);
  175.     }
  176.     if (write(fd[1], buffer, strlen(buffer) + 1) < 0) {
  177.         perror("write");
  178.         exit(1);
  179.     }
  180.     close(fd[1]);
  181.     pid = fork();
  182.     if (pid < 0) {
  183.         perror("fork");
  184.         exit(1);
  185.     }
  186.     if (pid == 0) {
  187.         args[0] = "wall";
  188.         args[1] = "-l";
  189.         args[2] = 0;
  190.         if (dup2(fd[0], 0) < 0) {
  191.         perror("dup2");
  192.         exit(0);
  193.         }
  194.         execv("/sprite/cmds/wall", args);
  195.         perror("execv");
  196.     } else {
  197.         wait(0);
  198.         sleep(sleepTime);
  199.     }
  200.     }
  201.     strncpy(buffer, rebootString, 100);
  202.     if (strlen(rebootString) > 0) {
  203.     reboot = 1;
  204.     }
  205.     if (dontSyncDisks) {
  206.     flags = 0;
  207.     } else {
  208.     flags = SYS_WRITE_BACK;
  209.     }
  210.     if (fastBoot) {
  211.     char    str[80];
  212.     int    fd;
  213.  
  214.     fd = open(FASTBOOT, O_CREAT, 0777);
  215.     if (fd < 0) {
  216.         if (errno != ENOENT) {
  217.         sprintf(str, "Couldn't open %s", FASTBOOT);
  218.         perror(str);
  219.         exit(1);
  220.         }
  221.     }
  222.  
  223.     strcat(buffer, " -f");
  224.     if (!halt && !debug) {
  225.         reboot = 1;
  226.     }
  227.     }
  228.     if (singleUser) {
  229.     strcat(buffer, " -s");
  230.     if (!halt && !debug) {
  231.         reboot = 1;
  232.     }
  233.     }
  234.     if (client) {
  235.     strcat(buffer, " -c");
  236.     if (!halt && !debug) {
  237.         reboot = 1;
  238.     }
  239.     }
  240.     if (rootcmds) {
  241.     strcat(buffer, " -x");
  242.     if (!halt && !debug) {
  243.         reboot = 1;
  244.     }
  245.     }
  246.     if (debugShutdown) {
  247.     fprintf(stderr, "Would call Sys_Shutdown(%s) here.\n", reboot ? buffer : "");
  248.     } else if (debug) {
  249.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_DEBUG, NULL);
  250.     } else if (reboot) { 
  251.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_REBOOT, buffer);
  252.     } else {
  253.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_HALT, NULL);
  254.     }
  255. }
  256. @
  257.  
  258.  
  259. 1.10
  260. log
  261. @added -q -S options, wall call
  262. @
  263. text
  264. @d11 1
  265. a11 1
  266. static char rcsid[] = "$Header: /sprite/src/cmds/shutdown/RCS/shutdown.c,v 1.9 89/06/19 14:33:33 jhh Exp Locker: douglis $ SP RITE (Berkeley)";
  267. d92 35
  268. a130 6
  269.     if (!quickBoot) {
  270.     sprintf(buffer, "echo system shutting down in %d seconds|wall -l",
  271.         sleepTime);
  272.     system(buffer);
  273.     sleep(sleepTime);
  274.     }
  275. d173 1
  276. a173 1
  277.     fprintf(stderr, "Would call Sys_Shutdown here.\n");
  278. @
  279.  
  280.  
  281. 1.9
  282. log
  283. @Added -x and -c options
  284. @
  285. text
  286. @d11 1
  287. a11 1
  288. static char rcsid[] = "$Header: /a/newcmds/shutdown/RCS/shutdown.c,v 1.8 89/05/21 16:49:06 jhh Exp Locker: jhh $ SP RITE (Berkeley)";
  289. d32 3
  290. d39 2
  291. d46 1
  292. a46 1
  293. char        rebootBuffer[100];
  294. d63 3
  295. d91 2
  296. a92 2
  297.     (void) Opt_Parse(argc, argv, optionArray, numOptions, 0);
  298.     strncpy(rebootBuffer, rebootString, 100);
  299. d96 6
  300. d120 1
  301. a120 1
  302.     strcat(rebootBuffer, " -f");
  303. d126 1
  304. a126 1
  305.     strcat(rebootBuffer, " -s");
  306. d132 1
  307. a132 1
  308.     strcat(rebootBuffer, " -c");
  309. d138 1
  310. a138 1
  311.     strcat(rebootBuffer, " -x");
  312. d143 3
  313. a145 1
  314.     if (debug) {
  315. d148 1
  316. a148 1
  317.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_REBOOT, rebootBuffer);
  318. @
  319.  
  320.  
  321. 1.8
  322. log
  323. @added -s and new handling of -f flag
  324. @
  325. text
  326. @d11 1
  327. a11 1
  328. static char rcsid[] = "$Header: shutdown.c,v 1.7 88/09/26 11:26:43 nelson Exp $ SP RITE (Berkeley)";
  329. d32 4
  330. a35 1
  331. static int    singleUser = 0;        /* Non-zero means reboot single user */
  332. d54 4
  333. a57 1
  334.     {OPT_TRUE, "s", (Address) &singleUser, "Reboot single user mode."},
  335. d82 2
  336. a83 2
  337.     
  338.     (void)Opt_Parse(argc, argv, optionArray, numOptions, 0);
  339. d94 12
  340. d113 12
  341. @
  342.  
  343.  
  344. 1.7
  345. log
  346. @Differentiates between a permission problem and a file bad directory
  347. problem when trying to open /fastboot
  348. @
  349. text
  350. @d11 1
  351. a11 2
  352. static char rcsid[] = "$Header: shutdown.c,v 1.6 88/08/16 11:27:44 nelson Exp $ SP
  353. RITE (Berkeley)";
  354. d32 1
  355. d38 1
  356. d51 1
  357. d78 1
  358. d88 3
  359. a90 10
  360.     char    str[80];
  361.     int    fd;
  362.  
  363.     fd = open(FASTBOOT, O_CREAT, 0777);
  364.     if (fd < 0) {
  365.         if (errno != ENOENT) {
  366.         sprintf(str, "Couldn't open %s", FASTBOOT);
  367.         perror(str);
  368.         exit(1);
  369.         }
  370. d92 3
  371. a98 1
  372.  
  373. d102 1
  374. a102 1
  375.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_REBOOT, rebootString);
  376. @
  377.  
  378.  
  379. 1.6
  380. log
  381. @Removed the -s option and changed -S to be -w.
  382. @
  383. text
  384. @d11 1
  385. a11 1
  386. static char rcsid[] = "$Header: shutdown.c,v 1.5 88/08/16 11:03:56 nelson Exp $ SP
  387. d19 1
  388. d90 5
  389. a94 3
  390.         sprintf(str, "Couldn't open %s", FASTBOOT);
  391.         perror(str);
  392.         exit(1);
  393. @
  394.  
  395.  
  396. 1.5
  397. log
  398. @Changed to halt by default.  Have to use the sync call now just to sync
  399. disk and not halt.
  400. @
  401. text
  402. @d11 1
  403. a11 1
  404. static char rcsid[] = "$Header: vmstat.c,v 1.3 88/08/15 15:53:19 nelson Exp $ SP
  405. a42 1
  406.     {OPT_TRUE, "S", (Address) &dontSyncDisks, "Dont sync the disks (Default is to sync the disks)"},
  407. d48 1
  408. @
  409.  
  410.  
  411. 1.4
  412. log
  413. @Can have not sync disks.  Useful for when rebooting.
  414. @
  415. text
  416. @d1 14
  417. a14 1
  418. #include "sprite.h"
  419. a15 1
  420. #include "fs.h"
  421. d17 2
  422. d22 15
  423. a36 7
  424. static Boolean dontSyncDisks = FALSE;
  425. static Boolean reboot = FALSE;
  426. static Boolean halt = FALSE;
  427. static Boolean debug = FALSE;
  428. static Boolean fastBoot = FALSE;
  429. char    nullString[] = "";
  430. char    *rebootString = nullString;
  431. d38 3
  432. d42 7
  433. a48 7
  434.     {OPT_TRUE, 'S', (Address) &dontSyncDisks, "Dont sync the disks (Default is to sync the disks)\n"},
  435.     {OPT_TRUE, 'r', (Address) &reboot, "Reboot"},
  436.     {OPT_STRING, 'R', (Address) &rebootString, "String to pass to boot prom (implies -r)"},
  437.     {OPT_TRUE, 'h', (Address) &halt, "Halt"},
  438.     {OPT_TRUE, 'd', (Address) &debug, "Enter the debugger"},
  439.     {OPT_TRUE, 'f', (Address) &fastBoot,
  440.          "Don't check disk consistency upon reboot (implies \"-h\" option if none other set)"},
  441. d52 16
  442. a71 2
  443.     ReturnStatus status;
  444.     int streamID;
  445. d74 3
  446. a76 3
  447.     (void) Opt_Parse(&argc, argv, numOptions, optionArray);
  448.     if (String_Length(rebootString) > 0) {
  449.     reboot = TRUE;
  450. d78 3
  451. a80 1
  452.     if (!dontSyncDisks) {
  453. a81 2
  454.     } else {
  455.     flags = 0;
  456. d84 8
  457. a91 10
  458.     status = Fs_Open(FASTBOOT, FS_CREATE, 0777, &streamID);
  459.     if (status == SUCCESS) {
  460.         status = Fs_Close(streamID);
  461.         if (status != SUCCESS) {
  462.         Stat_PrintMsg(status, "Error in Fs_Close");
  463.         Proc_Exit(status);
  464.         }
  465.     } else {
  466.         Stat_PrintMsg(status, "Shutdown: Fs_Open");
  467.         Proc_Exit(status);
  468. d93 2
  469. a94 2
  470.     if (! (debug || reboot || halt)) {
  471.         halt = TRUE;
  472. d97 1
  473. a97 1
  474.     
  475. d102 1
  476. a102 1
  477.     } else if (halt) { 
  478. a103 2
  479.     } else {
  480.     Sys_Shutdown(flags, NULL);
  481. @
  482.  
  483.  
  484. 1.3
  485. log
  486. @Made -R => -r.
  487. @
  488. text
  489. @d8 1
  490. a8 1
  491. static Boolean syncDisks = FALSE;
  492. d17 2
  493. a18 2
  494.     {OPT_TRUE, 's', (Address) &syncDisks, "Sync the disks and continue"},
  495.     {OPT_TRUE, 'r', (Address) &reboot, "Sync the disks and reboot"},
  496. d20 2
  497. a21 2
  498.     {OPT_TRUE, 'h', (Address) &halt, "Sync the disks and halt"},
  499.     {OPT_TRUE, 'd', (Address) &debug, "Sync the disks and enter the debugger"},
  500. d23 1
  501. a23 1
  502.          "Don't check disk consistency upon reboot (implies \"-h\" option, only because \"-r\" doesn't work)"},
  503. d33 1
  504. d39 5
  505. d62 1
  506. a62 1
  507.     Sys_Shutdown(SYS_KILL_PROCESSES | SYS_DEBUG, NULL);
  508. d64 1
  509. a64 1
  510.     Sys_Shutdown(SYS_KILL_PROCESSES | SYS_REBOOT, rebootString);
  511. d66 1
  512. a66 1
  513.     Sys_Shutdown(SYS_KILL_PROCESSES | SYS_HALT, NULL);
  514. d68 1
  515. a68 1
  516.     Sys_Shutdown(0, NULL);
  517. @
  518.  
  519.  
  520. 1.2
  521. log
  522. @Added fastboot.
  523. @
  524. text
  525. @d13 2
  526. d19 1
  527. d35 3
  528. d47 2
  529. a48 1
  530.         Stat_PrintMsg(status, "Warning: Fs_Open");
  531. d56 1
  532. a56 1
  533.     Sys_Shutdown(SYS_KILL_PROCESSES | SYS_DEBUG);
  534. d58 1
  535. a58 1
  536.     Sys_Shutdown(SYS_KILL_PROCESSES | SYS_REBOOT);
  537. d60 1
  538. a60 1
  539.     Sys_Shutdown(SYS_KILL_PROCESSES | SYS_HALT);
  540. d62 1
  541. a62 1
  542.     Sys_Shutdown(0);
  543. @
  544.  
  545.  
  546. 1.1
  547. log
  548. @Initial revision
  549. @
  550. text
  551. @d3 1
  552. d6 2
  553. d12 1
  554. d19 2
  555. d28 3
  556. d32 16
  557. @
  558.